import { createStore } from 'vuex'
import $ from 'jquery';
export default createStore({
  state: {
    id: "",
    username: "",
    email: "",
    phone: "",
    address: "",
    token: "",
    is_login: false,
    products: [],
    favorites: [],
    shopcarts: [],
    shopproducts: [],
    is_favorites: [],
    is_shopcarts: [],
    allproductprice: 0,
    orders: [],
    users: [],
  },
  getters: {
  },
  mutations: {
    update_token(state, token) {
      state.token = token;
    },
    update_info(state, data) {
      state.id = data.id;
      state.username = data.username;
      state.email = data.email;
      state.phone = data.phone;
      state.address = data.address;
      state.is_login = data.is_login;
    },

    logout(state) {
      state.is_login = false;
      localStorage.removeItem("token");
    },

    // update_productinfo(state,data){
    //         state.products=data;
    // },

    update_favoriteinfo(state, data) {
      state.favorites = data;
      for (let i in data) {
        state.is_favorites[data[i].id] = true;
      }
    },

    create_order(state, data) {
      state.orders.unshift({
        userid: data.userid,
        shopid: data.shopid,
        name: data.productname,
        orderprice: data.order_price,
        createtime: data.order_create,
        completetime: "未确认订单",
        ack: false,
      });
    },

    addfavorites(state, productid) {
      state.is_favorites[productid] = true;
      for (let i in state.products) {
        if (state.products[i].id === productid) {
          let temp = state.products[i];
          state.favorites.unshift(temp);
          break;
        }
      }
    },

    deletefavorite(state, productid) {
      state.favorites = state.favorites.filter(item => item.id !== productid);
      state.is_favorites[productid] = false;
    },

    update_shopcartsinfo(state, data) {
      state.shopcarts = data;
      for (let i in data) {
        state.is_shopcarts[data[i].id] = true;
      }
    },

    update_priceTozero(state) {
      state.allproductprice = 0;
    },

    deleteshopcart(state, productid) {
      state.shopcarts = state.shopcarts.filter(item => item.id !== productid);
      state.is_shopcarts[productid] = false;
    },

    addshopcarts(state, productid) {
      state.is_shopcarts[productid] = true;
      for (let i in state.products) {
        if (state.products[i].id === productid) {
          let temp = state.products[i];
          state.shopcarts.unshift(temp);
          break;
        }
      }
    },

    set_shopproducts(state, data) {
      state.shopproducts = data;
    },

    get_orders(state, data) {
      state.orders = data;
      for (let i in state.orders) {
        if (state.orders[i].completetime === "订单未确认") {
          state.orders[i].ack = false;
        }
        else {
          state.orders[i].ack = true;
        }
      }
    },

    updateuserinfo(state, data) {
      state.users = data;
    }

  },
  actions: {
    login(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/user/account/token/",
        type: "post",
        data: {
          username: data.username,
          password: data.password,
        },
        success(resp) {
          if (resp.error_message === "success") {
            localStorage.setItem("token", resp.token);
            context.commit("update_token", resp.token);
            data.success();
          } else { data.error(); }
        },
        error() {
          data.error();
        }
      })
    },

    getinfo(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/user/account/info/",
        type: "get",
        headers: {
          Authorization: "Bearer " + context.state.token,
        },
        success(resp) {
          if (resp.error_message === "success") {
            context.commit("update_info", {
              ...resp,
              is_login: true,
            });
            data.success(resp);
          } else {
            data.error();
          }
        },
        error() {
          data.error();
        }
      });
    },

    getproductinfo(context) {
      $.ajax({
        url: "http://8.134.248.229:10085/product/info/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        success(resp) {
          context.state.products = resp;
        }
      })
    },

    getfavoriteinfo(context) {
      $.ajax({
        url: "http://8.134.248.229:10085/favorite/get/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          userid: context.state.id,
        },
        success(resp) {
          context.commit("update_favoriteinfo", resp);
        }
      })
    },

    getshopcartsinfo(context) {
      //console.log("shopcarts");
      $.ajax({
        url: "http://8.134.248.229:10085/shopcarts/get/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          userid: context.state.id,
        },
        success(resp) {
          context.commit("update_shopcartsinfo", resp);
        }
      })
    },

    searchByname(context, data) {
      //console.log("search");
      $.ajax({
        url: "http://8.134.248.229:10085/product/search/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          search_content: data.search_content,
        },
        success(resp) {
          context.state.products = resp;
        }
      })
    },

    searchByprice(context, data) {
      console.log(data);
      $.ajax({
        url: "http://8.134.248.229:10085/product/searchByprice/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          min_price: data.min_price,
          max_price: data.max_price,
        },
        success(resp) {
          context.state.products = resp;
        }
      })
    },

    deleteshopcart(context, productid) {
      context.commit("deleteshopcart", productid);
      $.ajax({
        url: "http://8.134.248.229:10085/shopcarts/delete/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          productid: productid,
          userid: context.state.id,
        },
        success() {

        }
      })
    },

    getshopproduct(context, shopid) {
      $.ajax({
        url: "http://8.134.248.229:10085/shopproduct/get/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          shopid: shopid,
        },
        success(resp) {
          context.commit("set_shopproducts", resp);
        }
      })
    },

    create_order(context, data) {

      $.ajax({
        url: "http://8.134.248.229:10085/order/add/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          productname: data.productname,
          shopid: data.shopid,
          userid: data.userid,
          order_price: data.order_price,
          order_create: data.order_create,
        },
        success() {
          context.commit("create_order", data);
        }
      })
    },

    getorderinfo(context) {
      $.ajax({
        url: "http://8.134.248.229:10085/order/getinfo/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: { userid: context.state.id, },
        success(resp) {
          context.commit("get_orders", resp);
        }
      })
    },

    orderack(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/order/ack_completetime/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          orderid: data.orderid,
          completetime: data.completetime,
        },
        success() {

        }
      })
    },

    orderdelete(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/order/delete/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          orderid: data.orderid,
        },
        success() {

        }
      })
    },

    getshopidByproductid(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/sell/getshopid/",
        type: "get",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          productid: data.productid,
        },
        success(resp) {
          if (resp.error_message === "success")
            data.success(resp);
        }
      })
    },

    inventoryminus(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/product/inventoryminus/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          productid: data.id,
        },
      })
    },

    deleteproduct(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/product/delete/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          productid: data.productid,
        },
      })
    },

    deleteuser(context, userid) {
      $.ajax({
        url: "http://8.134.248.229:10085/user/delete/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          userid: userid,
        },
      })
    },

    changeuserinfo(context, data) {
      $.ajax({
        url: "http://8.134.248.229:10085/user/change/",
        type: "post",
        headers: { Authorization: "Bearer " + context.state.token, },
        data: {
          username: data.username,
          phone: data.phone,
          address: data.address,
          email: data.email,
          password: data.password,
          userid: context.state.id,
          flag: data.flag,
        },
        success(resp) {
          if (resp.error_message === "success") {
            context.commit("update_info", {
              ...data,
              is_login: true,
              id: context.state.id,
            })
            data.success();
          }
          else data.error(resp);
        }
      })
    }

  },
  modules: {
  }
})
